package com.github.binarywang.demo.wx.mp.handler;

import com.github.binarywang.demo.wx.mp.builder.TextBuilder;
import com.github.binarywang.demo.wx.mp.utils.JsonUtils;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Map;

import static me.chanjar.weixin.common.api.WxConsts.XmlMsgType;

/**
 * @author Binary Wang(https://github.com/binarywang)
 */
@Component
public class MsgHandler extends AbstractHandler {

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                    Map<String, Object> context, WxMpService weixinService,
                                    WxSessionManager sessionManager) {

        String msgType = wxMessage.getMsgType();
        String returnContent = null;
        if (!msgType.equals(XmlMsgType.EVENT)) {
            //此处可对 消息分类处理
            switch (msgType){

                case XmlMsgType.TEXT:
                    returnContent = "content：" + wxMessage.getContent();
                    break;
                case XmlMsgType.IMAGE:
                    returnContent = "picUrl:" + wxMessage.getPicUrl() + " ,mediaId :" + wxMessage.getMediaId();
                    break;
                case XmlMsgType.VOICE:
                    returnContent = "mediaId:" + wxMessage.getMediaId() + " ,format:" + wxMessage.getFormat() +
                        " ,recognition: " + wxMessage.getRecognition();
                    break;
                case XmlMsgType.SHORTVIDEO:
                case XmlMsgType.VIDEO:
                    returnContent = "ThumbMediaId:" + wxMessage.getThumbMediaId();
                    break;
                case XmlMsgType.LOCATION:
                    returnContent = "上报地理位置，Latitude : " + wxMessage.getLocationX() + " ，Longitude : " +
                        wxMessage.getLongitude() + " ，精度 :" + wxMessage.getScale() + " ,label: " + wxMessage.getLabel();
                    break;
                default:
                    returnContent = "收到用户信息内容：" + JsonUtils.toJson(wxMessage);
                    break;

            }

        }

        //当用户输入关键词如“你好”，“客服”等，并且有客服在线时，把消息转发给在线客服
//        try {
//            if (StringUtils.startsWithAny(wxMessage.getContent(), "你好", "客服")
//                //TODO 客服消息有问题
////                && weixinService.getKefuService().kfOnlineList()
////                .getKfOnlineList().size() > 0
//            ) {
//                return WxMpXmlOutMessage.TRANSFER_CUSTOMER_SERVICE()
//                    .fromUser(wxMessage.getToUser())
//                    .toUser(wxMessage.getFromUser()).build();
//            }
//        } catch (WxErrorException e) {
//            e.printStackTrace();
//        }

        return new TextBuilder().build(returnContent, wxMessage, weixinService);

    }

}
